<template>
    <view>
        <custom>
            <template #backText>每日奖励</template>
        </custom>
        <view class="cu-list menu">
            <nMenuItemContentAction>
                <template #content>
                    <view>
                        实际奖励 {{page.bonus_gold_bean_amount_1.true_bonus_gold_bean_amount | floor}}
                    </view>
                    <view class="text-nddyny text-sm">
                        今日奖励 {{page.bonus_gold_bean_amount_1.false_bonus_gold_bean_amount | floor}}
                    </view>
                    <view class="text-nddyny text-sm">
                        总计可领 {{collect_bonus_gold_bean_amount}}
                    </view>
                </template>
                <template #action>
                    <nButton v-if="page.bonus_gold_bean_times == 0" title="领取奖励" :tap="bonusGoldBean"></nButton>
                    <nButton v-else title="今日已领" :disabled="true"></nButton>
                </template>
            </nMenuItemContentAction>
            <nMenuItemContentAction>
                <template #content>
                    <text>我的金豆 {{$store.state.user.user_asset_info.gold_bean_amount | floor}}</text>
                </template>
                <template #action>
                    <nButton title="兑换金股" :tap="()=>{goldBean2GoldShareDialog.show = true}" class_button="light"></nButton>
                </template>
            </nMenuItemContentAction>
        </view>
        <nBar title="相关日志" right_title="查看全部日志" right_uri="/user/asset/log"></nBar>
        <userAssetLogTimeline ref="userAssetLogTimeline" :parentForm="userAssetLog.form"></userAssetLogTimeline>
        <modalDialog :show.sync="goldBean2GoldShareDialog.show" :confirm="goldBean2GoldShareBtn" title="兑换金股">
            <template #content>
                <nFormItem title="我的金豆" :value="goldBean2GoldShareDialogMyGoldBeanAmount" :class_value="(goldBean2GoldShareDialogMyGoldBeanAmount | isNaN)?'':'text-sm'"></nFormItem>
                <nFormItem title="兑换金股" format="number" :data.sync="goldBean2GoldShareDialog" field="gold_share_amount"></nFormItem>
            </template>
        </modalDialog>
    </view>
</template>

<script>
    import userAssetLogTimeline from '@/components/nddyny/userAssetLogTimeline.vue';

    export default {
        components: {
            userAssetLogTimeline,
        },
        data() {
            return {
                goldBean2GoldShareDialog: {
                    show: false,
                    formtag: {},
                    form: {
                        gold_share_amount: ''
                    },
                },
                userAssetLog: {
                    form: {
                        action_types: '40,52'
                    },
                },
                page: {
                    bonus_gold_bean_times: 0,
                    bonus_gold_bean_amount_1: {
                        true_bonus_gold_bean_amount: 0,
                        false_bonus_gold_bean_amount: 0
                    },
                },
            }
        },
        onReachBottom() {
            this.$refs.userAssetLogTimeline.getList(this.$api.list_action_add);
        },
        onLoad() {
            this.pageInit();
        },
        computed: {
            goldBean2GoldShareDialogMyGoldBeanAmount() {
                let my_gold_bean_amount = this.$nddyny.number.floor(this.$store.state.user.user_asset_info.gold_bean_amount);
                if (!this.goldBean2GoldShareDialog.form.gold_share_amount) {
                    return my_gold_bean_amount;
                }
                return my_gold_bean_amount +
                    ' - ' +
                    this.goldBean2GoldShareDialog.form.gold_share_amount * 1000 +
                    ' = ' + (this.$nddyny.bc.sub(my_gold_bean_amount, this.goldBean2GoldShareDialog.form.gold_share_amount *
                        1000));
            },
            collect_bonus_gold_bean_amount() {
                return this.$nddyny.number.floor(this.$nddyny.bc.sub(
                    this.$nddyny.bc.mul(
                        this.$store.state.user.user_asset_info.gold_share_amount,
                        100000
                    ),
                    this.$nddyny.bc.mul(
                        this.$store.state.user.user_asset_info.user_asset_append.false_bonus_gold_share_amount_history,
                        1000
                    )
                ));
            }
        },
        methods: {
            pageInit() {
                this.$api.post(this, {
                    url: '/user/dailyBonus/page',
                    success: (Result) => {
                        if (this.$api.form.toast(this, Result)) return;
                        this.page = Result.result;
                    }
                });
            },
            bonusGoldBean() {
                this.$api.post(this, {
                    loading: '领取中',
                    url: '/user/dailyBonus/goldBean',
                    success: (Result) => {
                        if (this.$api.form.toast(this, Result)) return;
                        this.pageInit();
                        this.$user.updateUserAssetInfo(this);
                        this.$refs.userAssetLogTimeline.getList(this.$api.list_action_force_init);
                    }
                });
            },
            goldBean2GoldShareBtn() {
                this.$api.post(this, {
                    loading: '兑换中',
                    url: '/user/asset/goldBean2GoldShare',
                    data_key: 'goldBean2GoldShareDialog',
                    success: (Result, data_key) => {
                        if (this.$api.form.toast(this, Result, data_key)) return;
                        this.pageInit();
                        this.$user.updateUserAssetInfo(this);
                        this.$refs.userAssetLogTimeline.getList(this.$api.list_action_force_init);
                        this.goldBean2GoldShareDialog.show = false;
                        this.goldBean2GoldShareDialog.form.gold_share_amount = '';
                        this.$store.commit('userToastSuccess', '兑换成功');
                    }
                });
            }
        },
    }
</script>
